package com.example.home.bus.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.bus.adapter.OrderAdapter;
import com.example.home.bus.bean.OrderBean;
import com.example.home.tool.OKHttpTool;

import org.json.JSONException;
import org.json.JSONObject;


public class OrderFragment extends Fragment {
    private RecyclerView rcvList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order, container, false);

        rcvList = view.findViewById(R.id.rcv_list);


        getRcvList();


        return view;
    }




    private void getRcvList() {
        OKHttpTool.doNetRequest("prod-api/api/bus/order/list",
                OKHttpTool.GET,
                OrderBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        OrderBean orderBean = (OrderBean) obj;
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindList(orderBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindList(OrderBean orderBean) {
        OrderAdapter orderAdapter = new OrderAdapter(getContext(), orderBean.getRows(), new OrderAdapter.CallBack() {
            @Override
            public void onItemClick(int position) {
                String orderNum = orderBean.getRows().get(position).getOrderNum();
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("orderNum",orderNum);
                    jsonObject.put("paymentType","电子支付");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if (orderNum != null){
                    OKHttpTool.doNetRequest("prod-api/api/bus/pay",
                            OKHttpTool.POST,
                            OrderBean.class,
                            new OKHttpTool.NetCallback() {
                                @Override
                                public void onSuccess(Object obj) {
                                    OrderBean orderBean = (OrderBean) obj;
                                    getActivity().runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            if (orderBean.getCode() == 200) {
                                                getRcvList();
                                                Toast.makeText(getActivity(),"支付成功",Toast.LENGTH_SHORT).show();
                                            }else {
                                                Toast.makeText(getActivity(),"支付失败",Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    });
                                }

                                @Override
                                public void onFailure(String msg) {

                                }
                            },jsonObject.toString());
                }
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        rcvList.setAdapter(orderAdapter);
        rcvList.setLayoutManager(linearLayoutManager);
    }
}